package com.suning.sawp.service.impl.dao.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.intf.dao.task.TaskHqDaoService;
import com.suning.sawp.po.task.TaskHq;
import com.suning.sawp.po.task.TaskRegionPo;
import com.suning.sawp.service.util.BeanUtil;
import com.suning.sawp.service.util.DateUtils;

@Service
public class TaskHqDaoServiceImpl implements TaskHqDaoService{

    @Resource
    DalClient dalClient;
    
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    @Override
    public int queryTaskHqCount(TaskHq queryCon) {
        Map<String,Object> paramMap = BeanUtil.describe(queryCon);
        if(queryCon.getStartTime() != null){
            paramMap.put("startTime",DateUtils.getDateStrByFormat(queryCon.getStartTime(), DATE_FORMAT));
        }
        if(queryCon.getEndTime() != null){
            paramMap.put("endTime",DateUtils.getDateStrByFormat(queryCon.getEndTime(), DATE_FORMAT));
        }
        Map<String, Object> numMap = dalClient.queryForMap("TASK_HQ.QUERY_COUNT_BY_CON", paramMap);
        return numMap != null ? ((Number)numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public List<TaskHq> queryTaskHqList(TaskHq queryCon, Integer pageNo, Integer pageSize) {
        Map<String,Object> paramMap = BeanUtil.describe(queryCon);
        if(queryCon.getStartTime() != null){
            paramMap.put("startTime",DateUtils.getDateStrByFormat(queryCon.getStartTime(), DATE_FORMAT));
        }
        if(queryCon.getEndTime() != null){
            paramMap.put("endTime",DateUtils.getDateStrByFormat(queryCon.getEndTime(), DATE_FORMAT));
        }
        if(pageNo != null && pageSize != null){
            paramMap.put("startIndex", (pageNo-1)*pageSize);
            paramMap.put("maxCount", pageSize);
        }
        return dalClient.queryForList("TASK_HQ.QUERY_BY_CON", paramMap,TaskHq.class);
    }

    @Override
    public Integer insertTask(TaskHq task) {
        return dalClient.persist(task).intValue();
    }

    @Override
    public boolean updateTask(TaskHq task, Integer Id) {
        Map<String,Object> paramMap = BeanUtil.describe(task);
        paramMap.put("condition_taskNo", Id);
        int num = dalClient.execute("TASK_HQ.UPDATE_BY_ID", paramMap);
        return num > 0;
    }

    @Override
    public TaskHq queryTaskById(Integer taskNo) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("taskNo", taskNo);
        return dalClient.queryForObject("TASK_HQ.QUERY_BY_CON", paramMap, TaskHq.class);
    }

    @Transactional
    @Override
    public void softDeleteTaskByTaskNo(Integer taskNo) {
        Map<String,Object> queryMap = new HashMap<String,Object>();
        queryMap.put("taskNo", taskNo);
        Map<String,Object> updateMap = new HashMap<String,Object>();
        updateMap.put("taskNo", taskNo);
        updateMap.put("taskStatus", TaskHq.TaskStatus.DELETE);
        dalClient.execute("TASK_HQ.SOFT_DELETE_HQ_REGION", updateMap);
        List<Integer> regionNos = dalClient.queryForList("TASK_HQ.SELECT_REGION_NO_BY_PAR_NO", queryMap,Integer.class);
        if(CollectionUtils.isNotEmpty(regionNos)){
            queryMap.put("parTaskNos", regionNos);
            List<Integer> storeNos = dalClient.queryForList("TASK_HQ.SELECT_STORE_NO_BY_PAR_NO", queryMap,Integer.class);
            if(CollectionUtils.isNotEmpty(storeNos)){
                Map<String,Object> updateStoreMap = new HashMap<String,Object>();
                updateStoreMap.put("taskNos", storeNos);
                updateStoreMap.put("taskStatus", TaskHq.TaskStatus.DELETE);
                dalClient.execute("TASK_HQ.SOFT_DELETE_STORE", updateStoreMap);
                queryMap.put("parTaskNos", storeNos);
                List<Integer> clerkNos =  dalClient.queryForList("TASK_HQ.SELECT_CLERK_NO_BY_PAR_NO", queryMap,Integer.class);
                if(CollectionUtils.isNotEmpty(clerkNos)){
                    Map<String,Object> updateClerkMap = new HashMap<String,Object>();
                    updateClerkMap.put("taskNos", clerkNos);
                    updateClerkMap.put("taskStatus", TaskHq.TaskStatus.DELETE);
                    dalClient.execute("TASK_HQ.SOFT_DELETE_CLERK", updateClerkMap);
                }
            }
        }
    }

    @Override
    public Integer insertTaskRegion(TaskRegionPo taskRegion) {
        return dalClient.persist(taskRegion).intValue();
    }

}
